<?php
/**
 * Created Time: 2017/1/21 15:25
 * Description :
 */

namespace app\services;


use Gregwar\Captcha\CaptchaBuilder;
use Gregwar\Captcha\PhraseBuilder;

class CaptchaService
{
    public $session;

    public static $code = 'code';

    public function Captcha($tmp)
    {
        $phrase = new PhraseBuilder;
        $code = $phrase->build(4); // 设置验证码位数
        $builder = new CaptchaBuilder($code, $phrase);
        $builder->setBackgroundColor(220, 210, 230);
        $builder->setDistortion(false);
        $builder->setMaxOffset(20);
        $builder->setMaxAngle(15);
        $builder->setMaxFrontLines(0);
        $builder->setMaxBehindLines(0);
        $builder->build($width = 80, $height = 40, $font = null);
        $phrase = $builder->getPhrase(); // 获取验证码的内容
        \Session::flash(self::$code, $phrase);// 把内容存入session
        header("Cache-Control: no-cache, must-revalidate");
        header("Content-Type:image/jpeg");
        // 生成图片
        return $builder->output();
    }
//        Route::get('kit/captcha.blade.php/{tmp}', 'KitController@captcha.blade.php');
    /**
     * @param $code
     * @return bool
     */
    public function verifyCaptcha($code)
    {
        return \Session::get(self::$code, '') == $code ? true : false;
    }
}